VR手柄的绘制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
[RequireComponent(typeof(SteamVR_RenderModel))]
public class HandTest : MonoBehaviour {
public enum HandType
{
LeftHand,
RightHand,
}
public HandType handType = HandType.RightHand;
uint index;
SteamVR_Controller.Device controller;
SteamVR_Events.Action newPosAction;
void Awake()
{
newPosAction = SteamVR_Events.NewPosesAction(OnNewPos);
}
void OnEnable()
{
newPosAction.enabled = true;
}
void OnDisable()
{
newPosAction.enabled = false;
}
IEnumerator Start()
{
while (controller == null)
{
yield return new WaitForSeconds(1.0f);
var system = OpenVR.System;
if (system != null)
{
if (handType == HandType.LeftHand)
{
index = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
}
else
{
index = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
}
if (index == OpenVR.k_unTrackedDeviceIndexInvalid)
{
continue;
}
SetDeviceIndex((int)index);
}
}
}
private void SetDeviceIndex(int index)
{
if (controller == null)
{
controller = SteamVR_Controller.Input(index);
SteamVR_RenderModel r = GetComponent<SteamVR_RenderModel>();
if(r)
{
r.SetDeviceIndex(index);
}
}
}
private void OnNewPos(TrackedDevicePose_t[] poses)
{
if (index == OpenVR.k_unTrackedDeviceIndexInvalid) return;
if (index >= poses.Length) return;
if(poses[index].bDeviceIsConnected && poses[index].bPoseIsValid)
{
var pos = new SteamVR_Utils.RigidTransform(poses[index].mDeviceToAbsoluteTracking);
transform.localPosition = pos.pos;
transform.localRotation = pos.rot;
}
}
}